package com.colorful.insuranceproject.claims;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
import android.support.v4.app.Fragment;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.IndicatorPagerAdapter;
import com.colorful.insuranceproject.base.TitleActivity;
import com.lovely3x.common.utils.ViewUtils;
import com.lovely3x.common.widgets.PagerSlidingTabStrip;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/11/7.
 * <p>
 * 理赔设置  和  进度查询的分支页面
 */
public class ClaimsTypeActivity extends TitleActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.siv_activity_task_details_indicator)
    PagerSlidingTabStrip psts;

    @Bind(R.id.vp_activity_task_details_pager)
    ViewPager viewPager;

    ArrayList<Fragment> listFragment;

    ArrayList<String> listName;

    IndicatorPagerAdapter adapter;

    /**
     * 用户选择的位置
     */
    private int position;


    @Override
    protected int getContentView() {
        return R.layout.activity_type;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onViewInitialized() {
        listFragment = new ArrayList<>();
        listName = new ArrayList<>();

        listName.add("步骤一：理赔设置");
        listName.add("进度查询");

        listFragment.add(StepOneFragment.newInstance());
        listFragment.add(ClaimsProgressFragment.newInstance());

        initViewPage();
    }

    private void initViewPage() {
        //设置对应的适配器
        adapter = new IndicatorPagerAdapter(getSupportFragmentManager(), listFragment, listName);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);

        //配置指示器
        psts.setShouldExpand(true);
        psts.setTabPaddingLeftRight(0);
        psts.setViewPager(viewPager);
        psts.setTextSize((int) getResources().getDimension(R.dimen.small_text));
        psts.setTextColorResource(R.drawable.filter_conditions_color);
        psts.setIndicatorHeight(ViewUtils.dp2pxF(2));
        psts.setIndicatorColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
        psts.setTypeface(null, Typeface.NORMAL);
        psts.setDividerColor(Color.TRANSPARENT);//未选中的指示器
        psts.setOnPageChangeListener(this);
        psts.reduceIndicatorWidth(ViewUtils.dp2pxF(40));
        psts.setUnderlineColor(Color.TRANSPARENT);

        //作用就是调整显示的界面状态和恢复界面
        viewPager.post(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(position);
                onPageSelected(position);
            }
        });
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 设置页面跳转的变化
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.position = position;
        final int count = psts.getTabCount();
        for (int i = 0; i < count; i++) {
            TextView tab = (TextView) psts.getTab(i);
            tab.setTextColor(ContextCompat.getColor(mActivity, R.color.auxiliary_text_bg));
        }

        TextView tab = (TextView) psts.getTab(position);
        tab.setTextColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));

        setTitle(position == 0 ? "理赔申请:票据上传" : "理赔申请:进度查询");
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
